<?php


namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\Token;
use \app\api\service\Subject as Subjectservice;
use app\lib\exception\SubjectException;
use app\lib\exception\SubjectExcption;
use think\console\output\question\Choice;

class Subject extends BaseController
{
    /**
     *获取分类
     * @param $course
     * @param $car_status
     * @return false|string
     * @throws SubjectExcption
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getType($course,$car_status){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->getTypeList($course,$car_status);
        if (empty($result)){
            throw new SubjectExcption([
                'msg'=>'获取分类失败',
                'error_code'=>100010
            ]);
        }
        return json_encode($result);
    }

    /*** 科目练习
     * @param $course 科目类型
     * @param $car_status  车辆类型
     * @return string
     * @throws SubjectExcption
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectExercises ($course,$car_status){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->SubjectExercises($uid,$course,$car_status);
        if (empty($result)){
            throw new SubjectExcption([
                'msg'=>'获取题目出错',
                'error_code'=>10010
            ]);
        }
        return json_encode($result);
    }

    /***对错判断
     * @param $course_id 题目ID
     * @param $status 题目对错 1：正确 2：错误
     * @param Choice
     * @param $car_status
     * @param $course
     * @param $num
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectJudgment($course_id,$status,$choice,$car_status,$course,$num){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->SubjectWhether($uid,$course_id,$status,$choice,$car_status,$course,$num);
        if (empty($result)){
            throw new SubjectException([
               'msg'=>'未知错误',
               'error_code'=>90000
            ]);
        }
        return json_encode($result);
    }

    /*** 错题查询
     * @return string
     * @throws SubjectExcption
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectWrongExercises(){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->SubjectWrongExercises($uid);
        if (empty($result)){
            throw new SubjectExcption([
               'msg'=>'您还没有错题可做哟',
               'error_code'=>800080
            ]);
        }
        return json_encode($result);
    }


    /***分类练习
     * @param $id
     * @param $course
     * @param $car_status
     * @return string
     * @throws SubjectExcption
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectSort($id,$course,$car_status){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->SubjectSort($id,$course,$car_status);
        if (empty($result)){
            throw new SubjectExcption([
                'code'=>100001,
                'msg'=>'未知错误',
            ]);
        }
        return json_encode($result);
    }


    /*** 随机练习
     * @param $course
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SubjectRandom($course){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->SubjectRandom($uid,$course);
        return json_encode($result);
    }

    /*** 上次随机练习的题目
     * @param $number
     * @return string
     */
    public function SubjectNumber($number){
        $uid = Token::getCurrentUid();
        $service = new Subjectservice();
        $result = $service->SubjectNumber($uid,$number);
        return json_encode($result);
    }
}